use aes::Aes128;
use block_modes::{BlockMode, Ecb};
use block_modes::block_padding;
type Aes128Ecb = Ecb<Aes128, block_padding::Pkcs7>;

pub fn encrypt(plaindata: &Vec<u8>, key: &[u8]) -> Vec<u8> {
    let iv = [0 as u8];
    // create an alias for convenience
    let cipher = Aes128Ecb::new_var(&key, &iv).unwrap();

    let ciphertext = cipher.encrypt_vec(&plaindata);
    ciphertext
}

pub fn decrypt(cipherdata: &Vec<u8>, key: &[u8]) -> Vec<u8> {
    let iv = [0 as u8];
    let cipher = Aes128Ecb::new_var(key, &iv).unwrap();
    let plaindata = cipher.decrypt_vec(cipherdata).unwrap();
    plaindata.to_vec()
}

#[cfg(test)]
mod tests {
    use hex_literal::hex;
    #[test]
    fn it_works() {
        let key = hex!("013F65A3AE564CA02D5215C36D71DBB3");

        let plaindata = b"test aes cfb";

        let cipherdata = super::encrypt(&plaindata.to_vec(), &key);
        let plaindata2 = super::decrypt(&cipherdata, &key);
        println!("{:?}", cipherdata);
        println!("{:?}", plaindata2);
        assert_ne!(cipherdata.len(), plaindata.len());
        assert_eq!(plaindata.to_vec() , plaindata2);
    }
}